"""
# 如果cookie中的sku_id在redis中不存在,那么直接新增到redis购物车hash字典
# 如果cookie中的sku_id在redis中存在,以cookie的count来覆盖redis中此sku_id的count

# 选中状态合并处理,两边都没勾选,最终还是不勾选
# 如果有一边是勾选,最终合并到redis就是勾选
"""
import pickle, base64
from django_redis import get_redis_connection


def merge_cart_cookie_to_redis(request, user, response):
    """
    合并购物车数据
    :param request: 借用以获取cookie购物车数据
    :param user: 想要获取redis hash和set
    :param response: 借用过来删除cookie
    :return: None
    """
    # 获取cookie购物车数据
    cart_str = request.COOKIES.get('cart')
    # 判断有没有cookie购物车数据,如果没有直接return
    if cart_str is None:
        return
    # 把cookie字符串转换成cart_dict
    cart_dict = pickle.loads(base64.b64decode(cart_str.encode()))
    # 创建redis连接对象
    redis_conn = get_redis_connection('cart')
    # 创建管道对象
    pl = redis_conn.pipeline()

    # 遍历cookie购物车大字典
    for sku_id in cart_dict:
        pl.hset('cart_%s' % user.id, sku_id, cart_dict[sku_id]['count'])
        if cart_dict[sku_id]['selected']:  # 如果cookie购物车当前sku_id是选中
            pl.sadd('selected_%s' % user.id, sku_id)

    # 执行管道
    pl.execute()

    # 清空cookie购物车数据
    # response.delete_cookie('cart')
    # return response


"""
{
    sku_id_16: {'count': 1, 'selected': True},
    sku_id_1: {'count': 1, 'selected': False},
}

{sku_id_1: 1, sku_id_16: 1}
{sku_id_1}
"""





#####################################################3
# import base64
# import pickle
#
# from django_redis import get_redis_connection
#
#
# def merge_cart_cookie_to_redis(request, response, user):
#     """
#     登录后合并cookie中的购物⻋车数据到redis中
#     :param request: 本次请求对象，获取cookie中的数据
#     :param response: 本次响应对象，清除cookie中的数据
#     :param user: 登录⽤用户信息，获取user_id
#     :return: response
#     """
#     # return response
#
#
#
# ################
#     # 获取cookie中的购物⻋车数据
#     cookie_cart_str = request.COOKIES.get('cart')
#
#     # cookie中没有数据就响应结果
#     if not cookie_cart_str:
#         return response
#
#     # 将cookie中的购物⻋车字符串串转成字典
#     cookie_cart_dict = pickle.loads(base64.b64decode(cookie_cart_str.encode()))
#
#
# #####################
#     # 获取redis中的购物⻋车数据
#     redis_conn = get_redis_connection('cart')
#     redis_cart_dict = redis_conn.hgetall('cart_%s' % user.id)
#     redis_cart_selected = redis_conn.smembers('selected_%s' % user.id)
#     # redis_cart_dict = {
#     #         b'sku_id':b'count',
#     #         b'sku_id':b'count'
#     #     }
#     new_redis_cart_dict = {}
#
#     for sku_id, count in redis_cart_dict.items():
#         new_redis_cart_dict[int(sku_id)] = int(count)
#
#
# ############################3
#     # 遍历cookie_cart_dict，将sku_id和count覆盖到new_redis_cart_dict
#     for sku_id, cookie_dict in cookie_cart_dict.items():
#         new_redis_cart_dict[sku_id] = cookie_dict['count']
#
#         if cookie_dict['selected']:
#             redis_cart_selected.add(sku_id)
#
#
# ###################################
#
#     # 将redis_cart_dict写⼊入到数据库
#     pl = redis_conn.pipeline()
#     pl.hmset('cart_%s' % user.id, new_redis_cart_dict)
#     pl.sadd('selected_%s' % user.id, *redis_cart_selected)
#     pl.execute()
#
#
# #######################################
#     # 清除cookie
#     response.delete_cookie('cart')
#     # 响应结果
#     return response
#
#
#
#
# def merge_cart_cookie_to_redis__1(request, user, response):
#     """
#     合并购物车数据
#     :param request: 借用以获取cookie购物车数据
#     :param user: 想要获取redis hash set
#     :param response:
#     :return:
#     """
#
#     cart_str = request.COOKIES.get('cart')
#
#     if cart_str is None:
#         return
#
#     cart_dict = pickle.loads(base64.b64decode(cart_str.encode()))
#
#     redis_conn = get_redis_connection('cart')
#
#     pl = redis_conn.pipeline()
#
#     # 遍历cookie购物车大字典
#     for sku_id in cart_dict:
#         pl.hset('cart_%s'%user.id, sku_id, cart_dict[sku_id]['count'])
#
#         if cart_dict[sku_id]['selected']:
#             pl.sadd('selected_%s'%user.id, sku_id)
#
#     # 执行管道
#     pl.execute()
#
#     # 清空cookie购物车数据
#
#